/****************************************************************************
**
****************************************************************************/

#ifndef OIOV_H
#define OIOV_H

#include <QThread>

class OiovResMain : public QThread
{
    Q_OBJECT

public:
    explicit OiovResMain(QObject *parent = nullptr);
    ~OiovResMain();

    static bool net_state()
    {
        return net_state_;
    }

protected:
    void run() Q_DECL_OVERRIDE;

private:
    qint32 loop();

private:
    bool m_quit;
    unsigned long m_cycle;

    static bool net_state_;
};

class OiovReqMain : public QThread
{
    Q_OBJECT

public:
    explicit OiovReqMain(QObject *parent = nullptr);
    ~OiovReqMain();

    //注：此处使用的是标记变量，也就是说线程间是通过标志来同步的，这种做法的隐患非常大，是不正规的同步方法。
    //        虽然使用了读写锁来强化程序，减少可能发生错误的概率，但这依然是有风险的线程同步方法。

    static void Updata_serial_number(uint8_t index);
    static void Int_pill_number(void);
    //Update
    static void SetStartUpdate(bool StartUpdate);

    static bool Check_Fee(quint8 gunno);
    static void SetCheck_Fee(quint8 gunno);
    static void ClearCheck_Fee(quint8 gunno);

    static bool Req_Fee( void);
    static void SetReq_Fee( void);
    static void ClearReq_Fee( void);

    static bool Charge_Handshake(quint8 gunno);
    static void SetCharge_Handshake(quint8 gunno);
    static void ClearCharge_Handshake(quint8 gunno);

    // 获取某个充电枪的充电完成
    static bool BMS_Charge_Complete(quint8 gunno);

    // 设置某个充电枪的充电完成
    static void SetBMS_Charge_Complete(quint8 gunno);

    // 清除某个充电枪的充电完成
    static void ClearBMS_Charge_Complete(quint8 gunno);
    // 获取某个充电枪的错误状态
    static bool BMS_Error(quint8 gunno);

    // 设置某个充电枪的错误状态
    static void SetBMS_Error(quint8 gunno);

    // 清除某个充电枪的错误状态
    static void ClearBMS_Error(quint8 gunno);

    static bool BMS_Set_Value(quint8 gunno);

    // 设置某个充电枪的错误状态
    static void SetBMS_Set_Value(quint8 gunno);

    // 清除某个充电枪的错误状态
    static void ClearBMS_Set_Value(quint8 gunno);
    // 获取某个充电枪的充电中止状态
    static bool BMS_Charge_Abort(quint8 gunno);

    // 设置某个充电枪的充电中止状态
    static void SetBMS_Charge_Abort(quint8 gunno);

    // 清除某个充电枪的充电中止状态
    static void ClearBMS_Charge_Abort(quint8 gunno);
    // 获取某个充电枪的充电停止状态
    static bool BMS_Charge_Stop(quint8 gunno);

    // 设置某个充电枪的充电停止状态
    static void SetBMS_Charge_Stop(quint8 gunno);

    // 清除某个充电枪的充电停止状态
    static void ClearBMS_Charge_Stop(quint8 gunno);
    // 获取某个充电枪的充电输出状态
    static int BMS_Charge_Output(quint8 gunno);

    // 设置某个充电枪的充电输出状态
    static void SetBMS_Charge_Output(quint8 gunno);

    // 清除某个充电枪的充电输出状态
    static void ClearBMS_Charge_Output(quint8 gunno);

    // 获取某个充电枪的充电过程状态
    static int BMS_Process_Status(quint8 gunno);

    // 设置某个充电枪的充电过程状态
    static void SetBMS_Process_Status(quint8 gunno);

    // 设置某个充电枪的充电过程状态
    static void ClearBMS_Process_Status(quint8 gunno);

    static bool  FirmwareStatusEvent(quint8 gunno);
    static void  SetFirmwareStatusEvent(quint8 gunno);
    static void  ClearFirmwareStatusEvent(quint8 gunno);



    //cmd
    static bool AppStartChargeService(quint8 gunno);
    static void SetAppStartChargeService(quint8 gunno);
    static void ClearAppStartChargeService(quint8 gunno);
    static bool AppStopChargeService(quint8 gunno);
    static void SetAppStopChargeService(quint8 gunno);
    static void ClearAppStopChargeService(quint8 gunno);
    static bool CardAuthStartChargeService(quint8 gunno);
    static void SetCardAuthStartChargeService(quint8 gunno);
    static void ClearCardAuthStartChargeService(quint8 gunno);

    //event
    static void SetCardAuthChargeEvent(quint8 gunno);
    static void SetStopChargeEvent(quint8 gunno);
    static void SetRebillChargeEvent();
    static void SetDeviceStatusEvent(quint8 gunno);
    static void SetHearbeatEvent(quint8 gunno);

    //property
    static void SetChargingProperty(quint8 gunno);
    static void ClearChargingProperty(quint8 gunno);

protected:
    void run() Q_DECL_OVERRIDE;

private:
    bool m_quit;
    unsigned long m_cycle = 500;

    //Update - flag
    static bool start_update_;

    //cmd - flag
    static QList<bool> app_start_charge_service_;
    static QList<bool> app_stop_charge_service_;
    static QList<bool> card_auth_start_charge_service_;

    //event - flag
    static QList<bool> card_auth_charge_event_;
    static QList<bool> stop_charge_event_;
    static       bool  rebill_charge_event_;
    static QList<bool> device_status_event_;
    static QList<bool> hearbeat_event_;
    static       bool  request_fee_event_;                 // 充电完成
    static QList<bool> check_fee_event_;                 // 充电完成
    static QList<bool> Charge_Handshake_;
    static QList<bool> firmware_status_event_;

    static QList<bool> BMS_Charge_Complete_;                 // 充电完成
    static QList<bool> BMS_Error_;                           // 错误
    static QList<bool> BMS_Set_Value_;                           // 错误
    static QList<bool> BMS_Charge_Abort_;                    // 充电中止
    static QList<bool> BMS_Charge_Stop_;                     // 充电停止
    static QList<bool> BMS_Charge_Output_;                    // 充电输出
    static QList<bool> BMS_Process_Status_;                   // 充电过程

    //property - flag
    static QList<bool> charging_property_ ;

private:
    //event
    static bool CardAuthChargeEvent(quint8 gunno);
    static bool StopChargeEvent(quint8 gunno);
    static bool RebillChargeEvent();
    static bool DeviceStatusEvent(quint8 gunno);
    static bool HearbeatEvent(quint8 gunno);
    static void ClearCardAuthChargeEvent(quint8 gunno);
    static void ClearStopChargeEvent(quint8 gunno);
    static void ClearRebillChargeEvent();
    static void ClearDeviceStatusEvent(quint8 gunno);
    static void ClearHearbeatEvent(quint8 gunno);

    //property
    static bool ChargingProperty(quint8 gunno);

signals:
    void StartUpdate(qint32 version, qint32 subversion, qint32 revision, qint32 packLength, qint32 checkSum, QString updateAddress);

public slots:

private slots:

};

#endif // OIOV_H
